package com.koron.css2.systemManage.servlet;

import com.github.pagehelper.PageInfo;
import com.koron.common.bean.query.OperationQueryBean;
import com.koron.css2.ApplicationConfig;
import com.koron.css2.ServerInterface;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.systemManage.bean.OperationTreeBean;
import com.koron.css2.systemManage.service.ServiceFactory;
import com.koron.util.Constant;
import com.koron.util.JsonUtils;
import com.koron.util.TreeUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.List;

public class OperationListTree implements ServerInterface{

	private static Logger log = LogManager.getLogger(OperationList.class);

	@Override
	public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
		MessageBean<List> msg = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "查询成功", List.class);
		try {
			OperationQueryBean query = JsonUtils.objectToPojo(req.getData(), OperationQueryBean.class);
			query.setApp(ApplicationConfig.getAppCustomerAppid());
			PageInfo<OperationTreeBean> page = ServiceFactory.getOperationService().listAllOperation(query);
			List<OperationTreeBean> data = TreeUtils.list2TreeList(page.getList(), "opCode", "parentOpCode", "children");
			msg.setData(data);
		} catch (Exception e) {
			log.error("查询菜单列表树出错：" + e);
			msg.setCode(Constant.MESSAGE_INT_FAIL);
			msg.setDescription("查询菜单列表树出错");
		}
		return msg;
	}

}
